home *** CD-ROM | disk | FTP | other *** search
/ Aminet 34 / Aminet 34 (2000)(Schatztruhe)[!][Dec 1999].iso / Aminet / util / gnu / unixcmds.lha / unixcmds / src / amigawildcard.c next >
Encoding:
C/C++ Source or Header  |  1999-10-06  |  1.8 KB  |  114 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include <dos/dos.h>
  5. #include <exec/memory.h>
  6. #include <exec/tasks.h>
  7. #include <proto/dos.h>
  8. #include <proto/exec.h>
  9. #include <pragmas/dos_pragmas.h>
  10. #include <string.h>
  11.  
  12. #include "amigawildcard.h"
  13.  
  14. #define ALLOC_STEP 50
  15.  
  16.  
  17. void init_list(t_strlist *strl)
  18. {
  19.    strl->string=0;
  20.    strl->len=0;
  21.    strl->len_alloc=0;
  22. }
  23.  
  24. void clear_list(t_strlist *strl)
  25. {
  26.    int i;
  27.  
  28.    for (i=0;i<strl->len;i++)
  29.    {
  30.      free (pop_elt(i,strl));
  31.    }
  32.  
  33.    strl->len=0;
  34.    if (strl->string!=0)  { free(strl->string); }
  35.    strl->string=0;
  36.  
  37. }
  38.  
  39. char *pop_elt(int index,t_strlist *strl)
  40. {
  41.   char *ret=NULL;
  42.  
  43.   if ((index>=0)&&(index<strl->len))
  44.   {
  45.     ret=strl->string[index].str;
  46.   }
  47.  
  48.  
  49.   return ret;
  50. }
  51.  
  52. static void push_elt(char *elt,t_strlist *strl)
  53. {
  54.    if (strl->len>=strl->len_alloc)
  55.    {
  56.       strl->len_alloc+=ALLOC_STEP;
  57.       strl->string=realloc(strl->string,strl->len_alloc*sizeof(t_str));
  58.    }
  59.  
  60.    strl->string[strl->len].str=strdup(elt);
  61.  
  62.    strl->len++;
  63. }
  64.  
  65. void print_list(t_strlist *strl)
  66. {
  67.   int i;
  68.   for (i=0;i<strl->len;i++)
  69.   {
  70.     puts(pop_elt(i,strl));
  71.   }
  72.   fflush(stdout);
  73. }
  74.  
  75. /* fill the list with program argv */
  76.  
  77. void fill_list(char **argv,int argstart,int argcount,t_strlist *strl)
  78. {
  79.   int i;
  80.   init_list(strl);
  81.  
  82.   for (i=argstart;i<argcount;i++)
  83.   {
  84.     scan_pattern(argv[i],strl);
  85.   }
  86.  
  87. }
  88.  
  89. int scan_pattern(char *source,t_strlist *strl)
  90. {
  91.     int ret;
  92.     struct AnchorPath *ap;
  93.     ap=(struct AnchorPath *)AllocMem(sizeof(struct AnchorPath),MEMF_PUBLIC|MEMF_CLEAR);
  94.     
  95.     if (!(ret=MatchFirst(source,ap)))
  96.     {
  97.       do
  98.       {
  99.         push_elt(ap->ap_Info.fib_FileName,strl);
  100.       }
  101.       while (!MatchNext(ap));
  102.  
  103.       MatchEnd(ap);
  104.     }
  105.     else
  106.     {
  107.         push_elt(source,strl);
  108.     }
  109.  
  110.     FreeMem(ap,sizeof(struct AnchorPath));
  111.  
  112.     return ret;
  113. }
  114.